package com.hnevc.jsondemo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2017/11/20.
 */

public class MyAdapter extends BaseAdapter {
    //定义一个成员变量，接收构造函数传递的数据
    List<Food> mFoodList;//是通过构造函数初始化其中的数据
    //上下文环境
    Context mContext;//要通过构造函数初始化

    public MyAdapter(List<Food> mFoodList,Context mContext) {
        this.mFoodList = mFoodList;
        this.mContext = mContext;
    }

    //返回要显示的数据项数目
    @Override
    public int getCount() {
        return mFoodList.size();
    }

    //返回第position位置的数据项
    @Override
    public Object getItem(int position) {
        return mFoodList.get(position);
    }

    //返回第position位置的数据项的关键字id
    @Override
    public long getItemId(int position) {
        return mFoodList.get(position).getFoodId();
    }

    //返回position位置的显示视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //1.从list_item中创建视图
        View view = View.inflate(mContext, R.layout.list_item, null);
        //2.修改list_item中的控件的属性值
            //获取id为id_iv_food_pic的图片显示视图控件
        ImageView imageView = (ImageView) view.findViewById(R.id.id_iv_food_pic);
        //获取id为id_iv_food_name的文本显示视图控件
        TextView textView = (TextView) view.findViewById(R.id.id_tv_food_name);
        //将position位置的食物的名字，设置为文本框的文本
        textView.setText(mFoodList.get(position).getFoodName());
        //3.返回视图
        return view;
    }
}
